import { GBox } from "../../../../com/mui/containers/GBox";
import { GTextItem } from "./GTextItem";
import { InvalidationType } from "../../../../fl/core/InvalidationType";
import { GTextFormat } from "../../../common/GTextFormat";
import { GBoxDirection } from "../../../../com/mui/containers/globalVariable/GBoxDirection";
import { EventName } from "../../mvc/EventName";
import { MuiEvent } from "../../../../com/mui/events/MuiEvent";
import { Vector } from "../../../../__AS3__/vec/Vector";
type int = number;
//class GTextBar
    
    export  class GTextBar extends GBox
    {
        constructor()
        {
            super();
            this.alltextItem = new Vector.<GTextItem>();
            return;
        }

        public set dataProvider(arg1: Array<any>)
        {
            if (this._dataProvider == arg1) 
            {
                return;
            }
            this._dataProvider = arg1;
            this._dataProviderChange = true;
            invalidate(InvalidationType.ALL);
            return;
        }

        public get dataProvider(): Array<any>
        {
            return this._dataProvider;
        }

        public set selectedIndex(arg1: int)
        {
            if (!(this._selectedIndex == arg1) || arg1 == 0) 
            {
                if (numChildren > this._selectedIndex) 
                {
                    (getChildAt(this._selectedIndex) as GTextItem).mouseEnabled = true;
                    (getChildAt(this._selectedIndex) as GTextItem).removeSelectedSkin();
                    if (this._normalTextformat != null) 
                    {
                        (getChildAt(this._selectedIndex) as GTextItem).textformat = this._normalTextformat;
                    }
                }
                this._selectedIndex = arg1;
                this._selectedChange = true;
                this.checkSelected();
                invalidate();
            }
            return;
        }

        public get selectedIndex(): int
        {
            return this._selectedIndex;
        }

        public set textformat(arg1: Object /* flash.text.TextFormat */)
        {
            if (arg1 == this._textformat) 
            {
                return;
            }
            this._textformat = arg1;
            return;
        }

        public set normalTextformat(arg1: Object /* flash.text.TextFormat */)
        {
            if (arg1 == this._normalTextformat) 
            {
                return;
            }
            this._normalTextformat = arg1;
            return;
        }

        public gettextItemAt(arg1: int): GTextItem
        {
            if (arg1 < this.alltextItem.length) 
            {
                return this.alltextItem[arg1];
            }
            return null;
        }

        public set textWidth(arg1: int)
        {
            this._textWidth = arg1;
            return;
        }

        public get textWidth(): int
        {
            return this._textWidth;
        }

        public set textHeight(arg1: int)
        {
            this._textHeight = arg1;
            return;
        }

        public get textHeight(): int
        {
            return this._textHeight;
        }

        public set selectedColor(arg1: uint)
        {
            this._selectedColor = arg1;
            return;
        }

        protected /* override */ updateDisplayList(): void
        {
            var loc1=null;
            var loc2=null;
            if (this._dataProviderChange) 
            {
                this.reset();
                var loc3=0;
                var loc4=this.dataProvider;
                for(loc2 of loc4) 
                {
                    loc1 = new GTextItem(loc2, this.textWidth, this.textHeight);
                    if (this._normalTextformat == null) 
                    {
                        loc1.textformat = new GTextFormat("", 12, loc2.color, null, null, null, null, null, flash.text.TextFormatAlign.CENTER);
                    }
                    else 
                    {
                        loc1.textformat = this._normalTextformat;
                    }
                    if (this.direction == GBoxDirection.HORIZONTAL) 
                    {
                        loc1.setVline();
                    }
                    loc1.colorSelected = this._selectedColor;
                    loc1.configEventListener(flash.events.MouseEvent.CLICK, this.onTextItemClickHandler);
                    addChild(loc1);
                    this.alltextItem.push(loc1);
                }
                this.checkSelected();
                this._dataProviderChange = false;
            }
            if (this._selectedChange) 
            {
                this._selectedChange = false;
                this.checkSelected();
            }
            super.updateDisplayList();
            dispatchEvent(new Object /* flash.events.Event */(EventName.RankBarInitEnd));
            return;
        }

        /* internal  */checkSelected(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < numChildren) 
            {
                loc1 = getChildAt(loc2) as GTextItem;
                if (this.selectedIndex != loc2) 
                {
                    if (!loc1.mouseEnabled) 
                    {
                        loc1.mouseEnabled = true;
                        if (this._normalTextformat != null) 
                        {
                            loc1.textformat = this._normalTextformat;
                        }
                    }
                }
                else 
                {
                    loc1.mouseEnabled = false;
                    loc1.addSelectedSkin();
                    if (this._textformat != null) 
                    {
                        loc1.textformat = this._textformat;
                    }
                    dispatchEvent(new MuiEvent(MuiEvent.GTABBAR_SELECTED_CHANGE, this.selectedIndex));
                }
                ++loc2;
            }
            return;
        }

        /* internal  */onTextItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as GTextItem;
            loc1.mouseEnabled = false;
            loc1.addSelectedSkin();
            if (this._textformat != null) 
            {
                loc1.textformat = this._textformat;
            }
            var loc2=getChildIndex(loc1);
            if (this._selectedIndex != loc2) 
            {
                (getChildAt(this._selectedIndex) as GTextItem).mouseEnabled = true;
                (getChildAt(this._selectedIndex) as GTextItem).removeSelectedSkin();
                if (this._normalTextformat != null) 
                {
                    (getChildAt(this._selectedIndex) as GTextItem).textformat = this._normalTextformat;
                }
                this._selectedIndex = loc2;
                dispatchEvent(new MuiEvent(MuiEvent.GTABBAR_SELECTED_CHANGE, loc2));
            }
            return;
        }

        protected reset(): void
        {
            while (numChildren > 0) 
            {
                (getChildAt(0) as GTextItem).removeEventListener(flash.events.MouseEvent.CLICK, this.onTextItemClickHandler);
                removeChild(getChildAt(0));
            }
            this.height = 0;
            this._selectedIndex = 0;
            this.alltextItem = new Vector.<GTextItem>();
            return;
        }

        public /* override */ dispose(arg1: Boolean=true): void
        {
            this.reset();
            super.dispose(arg1);
            return;
        }

        /* internal */ /* var */_dataProvider: Array<any>;

        /* internal */ /* var */_dataProviderChange: Boolean=false;

        /* internal */ /* var */_selectedIndex: int=0;

        /* internal */ /* var */_selectedChange: Boolean=false;

        /* internal */ /* var */_textformat: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_normalTextformat: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_textWidth: int=100;

        /* internal */ /* var */_textHeight: int=20;

        /* internal */ /* var */_selectedColor: uint=0;

        protected /* var */alltextItem: Vector.<GTextItem>;
    }
